package com.yuqing.sentimentapp.channel;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;

import com.yuqing.sentimentapp.R;
import com.yuqing.sentimentapp.base.BaseActivity;
import com.yuqing.sentimentapp.channel.helper.ItemDragHelperCallback;
import com.yuqing.sentimentapp.home.fragment.HomeFragment;
import com.yuqing.sentimentapp.login.bean.LoginResultBean;
import com.yuqing.sentimentapp.utils.SharePrefrenceHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.OnClick;

/**
 *  主页
 */

public class ChannelActivity extends BaseActivity {
    private RecyclerView mRecy;
    private String selectId;
    //我的栏目的数据
    private final ArrayList<ChannelData> myItems = new ArrayList<>();
    //其他栏目的数据
    final List<ChannelData> otherItems = new ArrayList<>();
    private ChannelAdapter adapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_channel;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void initView() {
        mRecy = (RecyclerView) findViewById(R.id.recy);
        initIntent();
        initData();
    }

    private void initIntent() {
        Intent intent = getIntent();
        if (intent != null) {
            selectId = intent.getStringExtra("selectId");
        }
    }

    private void initData() {
        LoginResultBean loginResultBean = SharePrefrenceHelper.getLoginResultBean();
        if (loginResultBean == null) return;
        List<ChannelData> userCategory = loginResultBean.getData().getUserCategory();
        if (userCategory == null) return;
        for (int i = 0; i < userCategory.size() ; i++){
            if (TextUtils.equals("1", userCategory.get(i).getStatus())) {
                myItems.add(userCategory.get(i));
            } else {
                otherItems.add(userCategory.get(i));
            }
        }

        //网格模式 4列
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this,4);
        mRecy.setLayoutManager(gridLayoutManager);

        //使用ItemTouchHelper
        ItemDragHelperCallback callback = new ItemDragHelperCallback();
        final ItemTouchHelper helper = new ItemTouchHelper(callback);//实现item的拖拽和滑动
        helper.attachToRecyclerView(mRecy);//通过attachToRecyclerView方法绑定RecyclerView
        //adapter对象
        adapter = new ChannelAdapter(this,helper,myItems,otherItems, selectId);
        //根据position来设置span size 这个我们的span count是4 如果是头我们就给span size 为4 内容就给1
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                int viewType = adapter.getItemViewType(position);
                //三元运算 如果是内容就给1 是头我们就给span size 为4
                return viewType == ChannelAdapter.TYPE_MY || viewType == ChannelAdapter.TYPE_OTHER ? 1 : 4;
            }
        });
        mRecy.setAdapter(adapter);

        //点击我的栏目中item 并toast提示
        adapter.setOnMyChannelItemClickListener((v, position) -> {
            Intent intent = new Intent();
            intent.putExtra(HomeFragment.RESULT_CHANNEL_DATA, myItems);
            intent.putExtra(HomeFragment.RESULT_CHANNEL_POSITION, position);
            setResult(HomeFragment.RESULT_CODE, intent);
            finish();
        });
    }

    @OnClick(R.id.iv_close)
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_close:
                finishReturnData();
                break;
        }
    }

    private void finishReturnData() {
        Intent intent = new Intent();
        intent.putExtra(HomeFragment.RESULT_CHANNEL_DATA, myItems);
        intent.putExtra(HomeFragment.RESULT_CHANNEL_POSITION, selectId);
        setResult(HomeFragment.RESULT_CODE, intent);
        //更新本地channelList
        adapter.updateChannelLocal();

        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finishReturnData();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
